<!--
 * @Author: your name
 * @Date: 2020-12-29 09:51:04
 * @LastEditTime: 2020-12-29 14:29:27
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edi
 * @FilePath: \basicvue\vuehtml\v-for.html
-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>v-for解决模板循环问题</title>
    <link rel="stylesheet" href="../assets/css/common.css">
    <script src="../assets/js/vue.js"></script>
    <style>
        #app h1{
            margin: 20px 0;
            border-bottom: 2px solid #ffffff;
        }
        #app ul{
            list-style-type: none;
            margin-bottom: 20px;
        }
        #app ul>li{
            width: 50%;
            height: 30px;
            background: #1abc9c;
            margin-bottom:10px;
            text-indent: 10px;
            color: #ffffff;
            line-height: 2em;
            font-weight: bold;
        }
        #app p{
            width: fit-content;
            color: #ffffff;
            font-size: 16px;
            padding-bottom: 5px;
            border-bottom: 2px solid #dddddd;
        }
    </style>
</head>
<body>
    <div id="app">
        <a href="../index.html" class="retu">BACK</a>
        <h1>v-for指令的用方法</h1>
        <ul>
            <li v-for="(item,index) in sortItems">当前的第{{ index+1 }}数据是 ：{{ item }}</li>
        </ul>
        <p>(1)：这是一个最基础的循环,先在js里面定义了items数组，然后在模板中使用v-for循环出来，但是需要注意的是，你需要的那个html标签,v-for就写在哪个上面</p>
        <p>(2)：数组自带的sort排序是有问题的,是按照第一个数字排序的，要解决这个问题，需要给sort函数中添加一个函数来解决排序问题</p>
        <h1>对象循环输出</h1>
        <ul>
            <li v-for="student in sortStudents">姓名：{{ student.name }}-- 年龄：{{ student.age }}</li>
        </ul>
    </div>

    <script type="text/javascript">
        const app = new Vue({
            el:"#app",
            data:{
                items : [10,11,15,9,8,5,3,2],
                students:[
                    {name:'haohao',age:15},
                    {name:'liuhao',age:50},
                    {name:'liuhouse',age:22},
                    {name:'gaobai',age:24},
                    {name:'phper',age:25}
                ]
            },
            computed:{
                //对items数组进行排序
                sortItems:function(){
                    return this.items.sort(sortNumber);
                },
                //对学生进行排序
                sortStudents:function(){
                    return sortStudents(this.students,'age');
                }
            }
        });
        

        /*
            对数组进行排序
        */
        function sortNumber(a,b){
            return b-a;
        }

        /*
         *对学生数组进行排序
         数组对象方法排序
        */
       function sortStudents(array,key){
            return array.sort(function(a,b){
                let x = a[key];
                let y = b[key];
                return ((x<y)?-1:((x>y)?1:0));
                // return x-y;
            });
       }
       
    </script>
    
</body>
</html>